<?php

namespace app\admin\controller\mk;
use app\admin\model\mk\GamePrize as wp;
use app\common\controller\Backend;
use app\admin\model\mk\GamePrizeRecord as gpr;
/**
 * 大转盘
 */
class Wheel extends Backend
{
    /**
     * Wheel模型对象
     * @var object
     * @phpstan-var \app\admin\model\mk\Wheel
     */
    protected object $model;

    protected array|string $preExcludeFields = ['id'];

    protected string|array $quickSearchField = ['id'];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new \app\admin\model\mk\Game();
        $this->request->filter('clean_xss');
    }

    public function index(): void
    {
        if ($this->request->param('select')) {
            $this->select();
        }

        list($where, $alias, $limit, $order) = $this->queryBuilder();
        $where[] = ['game.type','=','wheel'];


        $res = $this->model
            ->field($this->indexField)
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $arr = $res->items();
        foreach($arr as &$v){
              $v['kewan_num'] = (($v['limit_type']==0)?'每人可参与':'每人每天可参与').$v['limit_num']."次";
        }
        $this->success('', [
            'list'   => $arr,
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }

    /**
     * 若需重写查看、编辑、删除等方法，请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
     */

       function pro(){
          $act = input("post.act");
          $data = input("post.data");
          switch ($act) {
              case 'save':
                  if(!isset($data['type'])){
                      $this->success("error",['msg'=>'选择类型','code'=>0]);
                  }
                 
                  if((!isset($data['pro_id']) || $data['pro_id'] == '' || $data['pro_id'] == 0) && $data['type'] == 1){
                       $this->success("error",['msg'=>'请选择商品','code'=>0]);
                  }
                  if((!isset($data['quan']) || $data['quan'] == '' || $data['quan'] == 0) && $data['type'] == 2){
                       $this->success("error",['msg'=>'请选择优惠券','code'=>0]);
                  }
                   if((!isset($data['jifen']) || $data['jifen'] == '' || $data['jifen'] == 0) && $data['type'] == 3){
                       $this->success("error",['msg'=>'积分必填','code'=>0]);
                  }
                  if((!isset($data['money']) || $data['money'] == '' || $data['money'] == 0) && $data['type'] == 4){
                       $this->success("error",['msg'=>'金额必填','code'=>0]);
                  }
                  if(!isset($data['title']) || $data['title'] == ''){
                       $this->success("error",['msg'=>'奖品名称必须','code'=>0]);
                  }
                  if(!isset($data['probability']) ||  $data['probability'] == ''){
                       $this->success("error",['msg'=>'中奖概率必须','code'=>0]);
                  }
                  if(!isset($data['stock']) || $data['stock'] == ''){
                       $this->success("error",['msg'=>'库存必须','code'=>0]);
                  }
                  $data['guan_id'] = intval(input("post.id"));
                  $data['prize_num'] = $data['stock'];
                  wp::insert($data);
                  $this->success("ok",['code'=>1]);
                  break;
              case 'delete':
                 $id = input("post.id");
                 if($id){
                       $dd = wp::where("id",$id)->delete();
                       if($dd){
                              $this->success("1",['msg'=>'ok','code'=>1]);
                       }else{
                              $this->success("1",['msg'=>'失败','code'=>0]);
                       }
                 }
                 break;
              case 'prolist':
                   $id = input("post.id");
                   $res = wp::alias("s")->where("s.guan_id",$id)->join("goods p","p.id=s.pro_id","LEFT")->field("p.title,p.price,p.feng,s.*")->order("s.sort desc")->select();
                   $this->success("ok",$res);
                   break; 
              default:
                  // code...
                  break;
          }
    }


    function record(){
         $id = $this->request->param('id');
         if($id){
              $shop_id = $this->store_id;
              $res = gpr::alias("p")->join("user u",'u.id=p.user_id',"LEFT")->field("p.*,u.nickname")->where(['p.game_id'=>$id,'p.shop_id'=>$shop_id])->order("id desc")->select();
              foreach($res as &$v){
                  $v['time'] = date("Y-m-d H:i:s",$v['time']);
                  $v['ling_time'] = date("Y-m-d H:i:s",$v['ling_time']);
              }
         }
         $n = gpr::alias("p")->where(['p.game_id'=>$id,'p.shop_id'=>$shop_id])->count();
         $this->success("ok",['lists'=>$res ??[],'n'=>$n]);
    }
}